package com.gougou.manager.web.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConfig
 * @Description TODO
 * @Author c
 * @Date 2018/9/18 19:13
 * @Vcrsion 1.0
 **/
@Configuration
public class RabbitMqConfig {
    // 创建一个连接工厂
    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses("132.232.50.106:5672");
        connectionFactory.setUsername("gougou");
        connectionFactory.setPassword("gougou");
        connectionFactory.setVirtualHost("/gougou");
        connectionFactory.setPublisherConfirms(true);//消息确认
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    // 创建用户管理
    @Bean
    public RabbitAdmin rabbitAdmin(){
        return new RabbitAdmin(rabbitTemplate());
    }

    // 创建一个 topic 类型的交换器
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("GOUGOU-ITEM-EXCHANGE", true, false);
    }

    @Bean
    public Queue queue2(){
        return new Queue("GOUGOU-FRONT-CONTENT-QUEUE",true, false, false);
    }

    @Bean
    Binding bindingExchangeMessage() {        return BindingBuilder.bind(queue2()).to(topicExchange()).with("content.#");    }

    // 创建一个模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setExchange("GOUGOU-ITEM-EXCHANGE");
        return template;
    }
}
